﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using NesGfx;

namespace cnetmegamanprj
{
    public partial class tsaPreset : Form
    {
        readonly int mainMultiplier = 1, tileWidth = 8, tileHeight = 8;
        TSAEditData d;
        Bitmap b;
        Render render;
        public tsaPreset(TSAEditData d_)
        {
            InitializeComponent();
            d = d_;
            b = new Bitmap(tileWidth * d.displayC * (int)d.tsa.c * mainMultiplier, tileHeight * d.displayR * (int)d.tsa.r * mainMultiplier);
            render = new Render(d.chrdata, b, mainMultiplier);
        }

        private void tsaPreset_Load(object sender, EventArgs e)
        {
            pictureBox1.Image = b;
            pictureBox1.Width = b.Width;
            pictureBox1.Height = b.Height;
            this.Height = pictureBox1.Bottom + 42;
            draw();
        }
        void draw()
        {
            int w = (int)d.tsa.c * tileWidth * mainMultiplier;
            int h = (int)d.tsa.r * tileHeight * mainMultiplier;

            render.lockIt();
            //render.Draw(0, 0, 0);
            //render.Draw(d.tsa[0], 0 * w, 0 * h);

            for (int y = 0; y < d.displayR; y++)
            {
                for (int x = 0; x < d.displayC; x++)
                {
                    var palOff = d.highBits[y * d.displayC + x] * 4;
                    render.pal.setPalette(d.pal,palOff);
                    render.Draw(d.tsa[y * d.displayC + x], x * w, y * h, d.tileMap);
                }
            }//*/
            render.unlockIt();
            pictureBox1.Invalidate();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            draw();
        }
        int hc, hr;
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            hc = e.X / tileWidth;
            hr = e.Y / tileHeight;
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
        }

        private void tsaPreset_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape) this.Close();
        }

        private void pictureBox1_Click_1(object sender, EventArgs e)
        {

        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            new TileEditor(Form1.mm.getTileEditData()).ShowDialog();
            draw();
        }
    }
}
